//
//  KLineDataList.cpp
//  Chart
//
//  Created by CaoJun on 13-6-17.
//
//

#include "KLineDataList.h"
#include "KLineData.h"
#include "TimeKLineData.h"
#include "DateKLineData.h"

using namespace ytx;
using namespace std;

KLineDataList * KLineDataList::instance(NULL);

KLineDataList :: KLineDataList(vector<Metal> & metalList, AVGDataList * avgDataList)
{
    create(metalList, avgDataList);
}

KLineDataList::~KLineDataList()
{
    
}

void KLineDataList :: create(vector<Metal> & metalList, AVGDataList * avgDataList)
{
    Config * config = Config::getInstance();
    int len = (int)metalList.size();
    int limit = config->kLine.getCount + Config::getInstance()->kLine.addtionalK;
    //Attention: 减去8小时 原来是localtime
    YTXTime tradedate = YTXTime().GMT8();
    
    for(int i = 0;i<len;i++)
    {
        Metal & metal = metalList[i];
        
        for(int j = LineType::L5m;j != LineType::EMPTY;j++){
            LineType::LineType qType = static_cast<LineType::LineType>(j);
            
            if(LineType::isMinuteLine(qType) || qType == LineType::L1d){
                TimeKLineData * tempK = new TimeKLineData(metal.getId(), metal.getCurrentDate(), qType, metal.getMarketType(), metal.getToFix(), metal.getName(), metal.getNickname(), metal.isHasVolume(), limit, metal.getThreshold(), metal.getBuffer(), tradedate, avgDataList->get(metal.getId()));
                tempK->listenOn();
                tempK->setIndexConfig(metal.getIndexConfig());
                set(tempK);
                
                if(qType == LineType::L1d){
                    dayK = tempK;
                }
            }
            else{
                DateKLineData * tempK = new DateKLineData(metal.getId(), metal.getCurrentDate(), qType, metal.getMarketType(), metal.getToFix(), metal.getName(), metal.getNickname(), metal.isHasVolume(), limit, metal.getThreshold(), metal.getBuffer(), tradedate, dayK);
                tempK->listenOn();
                tempK->setIndexConfig(metal.getIndexConfig());
                set(tempK);
            }
            
            
        }
        
    }
    
}

KLineDataList * KLineDataList::getInstance(vector<Metal> & metalList, AVGDataList * avgDataList)
{
    if(KLineDataList::instance == NULL)
    {
        KLineDataList::instance = new KLineDataList(metalList, avgDataList);
    }
    return KLineDataList::instance;
}

KLineDataList * KLineDataList::getInstance()
{
    return KLineDataList::instance;
}



